<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys"%>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>${tabTitle}</title>
    <jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div>
        <div id="pageInTab" class="pageInTab">
            <form id="ins_cfg_info_edit_form">
                <div class="fieldset1 tabel_col clearFix">
                    <div class="fieldBox col_50">
                        <div class="label">客户姓名</div>
                        <div class="field">
                            <span >${cstNm}</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">身份证号</div>
                        <div class="field">
                            <span >${certNo}</span>
                        </div>
                    </div>
                    <div class="fieldBox col_100">
                        <div class="label">合同号</div>
                        <div class="field">
                            <span >${lsCntNo}</span>
                        </div>
                    </div>
                    <div>
                        <div class="label" style="color: blue;">账户详情</div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">开户银行</div>
                        <div class="field">
                            <select name="bkCd" id="bkCd" value="${bkCd}" validate="{required:true}">
                                <c:if test="${bkCd == null || bkCd == ''}">
                                    <option value="" selected="selected">--请选择--</option>
                                </c:if>
                                <c:if test="${bkCd != null && bkCd != ''}">
                                    <option value="">--请选择--</option>
                                </c:if>
                                <c:forEach items="${bankList}" var="item">
                                    <c:if test="${item.key == bkCd}">
                                        <option value="${item.key}" selected="selected">${item.value}</option>
                                    </c:if>
                                    <c:if test="${item.key != bkCd}">
                                        <option value="${item.key}">${item.value}</option>
                                    </c:if>
                                </c:forEach>
                            </select>
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">银行账号</div>
                        <div class="field">
                            <input name="accNo" id="accNo" type="text" class="ui-textbox" value="${accNo}" validate="{number:true, maxlength:22, minlength:15}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">预留手机号</div>
                        <div class="field">
                            <input name="mp" id="mp" type="text" class="ui-textbox" value="${mp}" validate="{number:true, maxlength:11, minlength:11}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50" id="verdictStatusDiv">
                        <div class="label">签约状态</div>
                        <div class="field">
                            <span>${accResCd}</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50" id="bkCdDiv">
                        <div class="label">开户行号</div>
                        <div class="field">
                            <span >${bkCd}</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50" id="verificationCodeDiv">
                        <div class="label">验证码</div>
                        <div class="field">
                            <input name="verificationCode" id="verificationCode" type="text" class="ui-textbox" value="">
                        </div>
                    </div>
                    <div class="fieldBox col_100" style="display: none;">
                        <div class="field">
                            <input name="prjId" id="prjId" type="text" class="ui-textbox" value="${prjId}">
                            <input name="cstId" id="cstId" type="text" class="ui-textbox" value="${cstId}">
                            <input name="cstNm" id="cstNm" type="text" class="ui-textbox" value="${cstNm}">
                            <input name="fundRasInsId" id=fundRasInsId type="text" class="ui-textbox" value="${fundRasInsId}">
                        </div>
                    </div>
                </div>

                <div id="saveInsCfgDiv" class="btnsBox">
                    <span id="msmCodeCfg" class="sq-button sq-button-round" onclick="return getVerificationCode();"  style="display: none">获取验证码</span>
                    <span id="unBundCard" class="sq-button sq-button-round " onclick="return unBindCard();"  style="display: none">解绑</span>
                    <span id="saveInsCfg" class="sq-button sq-button-round " onclick="return saveInfo();"  style="display: none">绑卡</span>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    var verdictStatus = "${verdictStatus}";
    var detailFlag = '${detailFlag}';
    var prjId = '${prjId}';
    var accNo = '${accNo}';
    var insCode = '${insCode}';
    var accInfoId = '${accInfoId}';
    var fundRasInsId = '${fundRasInsId}';
    var cstId = '${cstId}';
    var certNo = '${certNo}';
    var cstNm = '${cstNm}';
    $(function () {
        initData();

        $("#verificationCode").change(function (){
            var _verificationCode = $("#verificationCode").val();
            if (detailFlag == '2' && insCode == 'zgcbank' && StringUtils.isNotEmpty(_verificationCode)){
                $("#unBundCard").removeAttr("style");
            } else if (detailFlag == '3' && StringUtils.isNotEmpty(_verificationCode)){
                $("#saveInsCfg").removeAttr("style");
            }
        });
    });

    function initData() {
        $("#ins_cfg_info_edit_form").ligerForm({
            validate: true
        });
        if (detailFlag === '1'){
            $("#verificationCodeDiv").hide();
            liger.get("bkCd").setDisabled(true);
            liger.get("accNo").setDisabled(true);
            liger.get("mp").setDisabled(true);
            if ("ylbank" === insCode || "zgcbank" == insCode){
                $("#bkCdDiv").hide();
            }
        } else if (detailFlag == '2'){
            $("#bkCdDiv").hide();
            liger.get("bkCd").setDisabled(true);
            liger.get("accNo").setDisabled(true);
            liger.get("mp").setDisabled(true);
            if (insCode == 'tianjinbank'){
                $("#verdictStatusDiv").show();
                $("#verificationCodeDiv").hide();
                if ('${accResCd}' == '已签约'){
                    $("#unBundCard").removeAttr("style");
                }
            } else if (insCode == 'zgcbank'){
                $("#verificationCodeDiv").hide();
                if ('${accResCd}' != '已解绑'){
                    $("#msmCodeCfg").removeAttr("style");
                    $("#verificationCodeDiv").show();
                }
            }
        } else if (detailFlag == '3'){
            $("#msmCodeCfg").removeAttr("style");
            $("#verdictStatusDiv").hide();
            $("#bkCdDiv").hide();
            $("#bkCd").val("");
            $("#accNo").val("");
            $("#mp").val("");
        }
    }

    //解绑
    function unBindCard(){
        if (StringUtils.isEmpty(insCode) || StringUtils.isEmpty(prjId) || StringUtils.isEmpty(accInfoId)) {
            top.$.ligerDialog.SQalert({content: "项目id、银行卡号、客户账户id不能为空!", SQ_type: "error"});
            return false;
        }
        var wait_dialog;
        $.ligerDialog.SQconfirm('确定解绑吗？', function (yes) {
            if (yes == false) {
                return;
            }
            var _url = "";
            if (insCode == 'tianjinbank'){
                _url = '<%=path%>/institution/insChangeCrad/unbindCard.do';
                $.ajax({
                    type:"post",
                    url: _url,
                    data: {
                        'prjId' : prjId,
                        'insCode': insCode,
                        'accInfoId': accInfoId
                    },
                    dataType:"json",
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            UIUtils.alertSuccessHold1(msg.info);
                            $("#unBundCard").attr("style","display: none");
                        } else {
                            UIUtils.alertErrorHold1(msg.info);
                        }
                    }
                });
            } else if (insCode == 'zgcbank'){
                _url = '<%=path%>/institution/zgcbank/exchangeCardSave.do';
                var data = getData();
                data["changeTypeCd"] = "0";//解绑
                $.ajax({
                    type:"post",
                    url: _url,
                    data: data,
                    dataType:"json",
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            UIUtils.alertSuccessHold1(msg.info);
                            $("#unBundCard").attr("style","display: none");
                        } else {
                            UIUtils.alertErrorHold1(msg.info);
                        }
                    }
                });
            }
        });
    }

    function getData(){
        var ligerForm = liger.get("ins_cfg_info_edit_form");
        var data = ligerForm.getData();
        data["bkCd"] = liger.get("bkCd").selectedValue;
        data["bkNm"] = liger.get("bkCd").selectedText;
        data["prjId"] = prjId;
        data["cstId"] = cstId;
        data["insCode"] = insCode;
        data["fundRasInsId"] = fundRasInsId;
        data["insId"] = fundRasInsId;
        data["accInfoId"] = accInfoId;
        data["certNo"] = certNo;
        data["cstNm"] = cstNm;
        data["accNo"] =  $("#accNo").val();
        data["mp"] = $("#mp").val();
        data["verificationCode"] = $("#verificationCode").val();//验证码
        data["changeTypeCd"] = "1";//解绑
        return data;
    }

    //获取验证码
    function getVerificationCode(){
        if($("#msmCodeCfg").attr("isSend") == '1'){
            return;
        }
        var ligerForm = liger.get("ins_cfg_info_edit_form");
        if (!ligerForm.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
            return false;
        }
        if(!$("#bkCd").val()){
            top.$.ligerDialog.SQalert({content: "请选择开户银行！", SQ_type: "error"});
            return false;
        }
        if(!$("#accNo").val()){
            top.$.ligerDialog.SQalert({content: "银行账号不能为空，请补充！", SQ_type: "error"});
            return false;
        }
        if(!$("#mp").val()){
            top.$.ligerDialog.SQalert({content: "预留手机号不能为空，请补充！", SQ_type: "error"});
            return false;
        }
        settime($("#msmCodeCfg"));
        var data = getData();
        if (detailFlag == '2'){
            data['changeypeCd'] = 'JB';
        } else if (detailFlag == '3'){
            data['changeypeCd'] = 'BK';
        }
        var wait_dialog;
        var url = '<%=path%>/institution/insChangeCrad/getVerificationCode.do';
        if (insCode == 'zgcbank'){
            url = '<%=path%>/institution/zgcbank/getVerificationCodeForZgcbank.do';
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: url,
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    $("#uniqueCode").val(msg.data.uniqueCode);
                    UIUtils.alertSuccessHold1(msg.info);
                } else if(msg.success == 2){
                    UIUtils.alertSuccessHold1('该银行卡已签约，请联系管理员进行解绑操作');
                } else {
                    UIUtils.alertErrorHold1(msg.info);
                }
            },
            error:function () {
                UIUtils.alertErrorHold1("验证码发送失败");
            }
        });
    }

    var countdown = 60;

    function settime(obj) {
        //发送验证码倒计时
        if (countdown == 0) {
            $(obj).removeClass("disabledBtn")
            $(obj).attr("style", "background: ");
            $(obj).attr("isSend", "0");
            $(obj).text("获取验证码");
            countdown = 60;
            return;
        } else {
            $(obj).addClass("disabledBtn")
            $(obj).attr("style", "background: gray;");
            $(obj).attr("isSend", "1");
            $(obj).text("重新发送(" + countdown + ")");
            countdown--;
        }
        setTimeout(function () {
            settime($(obj))
        }, 1000)
    }

    frameElement.dialog.onOk = function () {
        saveInfo();
    }

    //确认绑卡
    function saveInfo() {
        var ligerForm = liger.get("ins_cfg_info_edit_form");
        if (!ligerForm.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
            return false;
        }

        if(!$("#bkCd").val()){
            top.$.ligerDialog.SQalert({content: "请选择开户银行！", SQ_type: "error"});
            return false;
        }
        if(!$("#accNo").val()){
            top.$.ligerDialog.SQalert({content: "银行账号不能为空，请补充", SQ_type: "error"});
            return false;
        }
        if(!$("#mp").val()){
            top.$.ligerDialog.SQalert({content: "预留手机号不能为空，请补充！", SQ_type: "error"});
            return false;
        }
        if(!$("#verificationCode").val()){
            top.$.ligerDialog.SQalert({content: "验证码不能为空，请补充！", SQ_type: "error"});
            return false;
        }
        var data = getData();
        var wait_dialog;
        if (insCode == 'tianjinbank'){
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data,
                url: '<%=path%>/institution/insChangeCrad/exchangeCardSave.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        //不关闭当前页面,刷新父页面
                        parent.grid.reload();
                        $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                        frameElement.dialog.close();
                    } else {
                        UIUtils.alertErrorHold1(msg.info);
                    }
                },
                error:function(e){
                    wait_dialog.close();
                    UIUtils.alertErrorHold1("系统异常！");
                }
            });
        } else if (insCode == 'zgcbank'){
            data["changeTypeCd"] = "1";//绑卡
            $.ajax({
                type:"post",
                url: '<%=path%>/institution/zgcbank/exchangeCardSave.do',
                data: data,
                dataType:"json",
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        //不关闭当前页面,刷新父页面
                        parent.grid.reload();
                        $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                        frameElement.dialog.close();
                    } else {
                        UIUtils.alertErrorHold1(msg.info);
                    }
                }
            });
        }
    }




</script>

</body>
</html>